package cn.wolfcode.car.business.web.controller.employee;

import cn.wolfcode.car.business.domain.Department;
import cn.wolfcode.car.business.domain.Employee;
import cn.wolfcode.car.business.query.EmployeeQuery;
import cn.wolfcode.car.business.service.DepartmentService;
import cn.wolfcode.car.business.service.EmployeeService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.StringUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

/**
 * @Author WaveHi
 * @Date 19/11/2021 下午 2:29
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@Controller
@RequestMapping("/business/employee")
public class EmployeeController {

    private final String prefix = "business/employee/";

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/listPage")
    public String listPage(Model model) {
        model.addAttribute("departments", departmentService.getAllDepartments());
        return prefix + "list";
    }

    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Employee> query(
            EmployeeQuery employeeQuery) {
        //Judge data
        boolean keyEmpty = StringUtils.isEmpty(employeeQuery.getKey());
        //Correct data
        if(!keyEmpty) {
            employeeQuery.setKey(employeeQuery.getKey().trim());
        }else {
            employeeQuery.setKey(null);
        }
        //Query
        return employeeService.getPartOfEmployees(employeeQuery);
    }

    @RequestMapping("/addPage")
    public String addPage(Model model) {
        model.addAttribute("departments", departmentService.getAllDepartments());
        return prefix + "add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(Employee employee) {
        //Judge data
        boolean nameIsEmpty = StringUtils.isEmpty(employee.getName());
        boolean emailEmpty = StringUtils.isEmpty(employee.getEmail());

        if(!nameIsEmpty && employee.getHiredate() != null) {
            //Correct data
            employee.setName(employee.getName().trim());
            if(!emailEmpty) {
                employee.setEmail(employee.getEmail().trim());
            }
            //Add
            employeeService.add(employee);
            return AjaxResult.success();
        }
        return AjaxResult.error("所需信息不足！");
    }

    @RequestMapping("/editPage")
    public String editPage(Model model,Long id) {
        model.addAttribute("departments", departmentService.getAllDepartments());
        if(id != null) {
            model.addAttribute("employee", employeeService.getEmployee(id));
        }
        return prefix + "edit";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult update(Employee employee) {
        //Judge data
        boolean nameIsEmpty = StringUtils.isEmpty(employee.getName());
        boolean emailEmpty = StringUtils.isEmpty(employee.getEmail());

        if(employee.getId() != null && !nameIsEmpty && employee.getHiredate() != null) {
            //Correct data
            employee.setName(employee.getName().trim());
            if(!emailEmpty) {
                employee.setEmail(employee.getEmail().trim());
            }
            //Update
            employeeService.update(employee);
            return AjaxResult.success();
        }
        return AjaxResult.error("所需信息不足");
    }

    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids) {
        if(ids != null && ids.length > 0) {
            employeeService.remove(ids);
            return AjaxResult.success();
        }
        return AjaxResult.error("需提供雇员id！");
    }

    @RequestMapping("/showDepartment")
    public String showDepartment(Model model,Long id) {
        if(id != null) {
            model.addAttribute("department", departmentService.getDepartment(id));
        }
        return prefix + "showDepartment";
    }

}
